import 'package:flutter/material.dart';
import 'package:x3_charts_core/model/two_way_bar_chart_model.dart';

///**
///* @author hrongyong
///* @date   2/4/21
///* @email hrongyong@gmail.com
///* Description: 双向横条图
///*/
class TwoWayBarChartPainter extends CustomPainter {
  final Animation<double> repaint;

  ///左边数据
  List<TwoWayBarChartModel> leftList;

  ///右边数据
  List<TwoWayBarChartModel> rightList;

  ///左边颜色
  Color leftColors;

  ///右边颜色
  Color rightColors;

  TwoWayBarChartPainter({
    @required this.repaint,
    @required this.leftList,
    @required this.rightList,
    this.leftColors = const Color(0xFFFFE3D8),
    this.rightColors = const Color(0xFF48AACA),
  }) : super(repaint: repaint);

  double maxWidth;
  double maxHeight;
  double yStep;

  ///共有多少个横条
  int many = 3;

  Paint fillPaint = Paint()..color = Colors.red;

  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);

  @override
  void paint(Canvas canvas, Size size) {
    maxWidth = size.width;
    maxHeight = size.height;
    yStep = maxHeight / many;

    ///移动绘画中心
    canvas.translate(size.width / 2, 0);

    ///绘制左边
    drawLeft(canvas, size);

    ///绘制右边
    drawRight(canvas, size);

    ///绘制Title
    drawTitle(canvas, size);

    ///绘制Title
    drawValueString(canvas, size);
  }

  drawValueString(Canvas canvas, Size size) {
    canvas.save();

    //左边右边
    for (int i = 0; i < leftList.length; i++) {
      canvas.save();
      double dy = yStep * 0.75 / 2 + yStep * i;
      canvas.translate(0, dy);

      double leftWidth = leftList[i].proportion * maxWidth / 2;
      double rightWidth = rightList[i].proportion * maxWidth / 2;
      if (leftWidth < 40) {
        _drawAxisValueText(canvas, leftList[i].valueString, offset: Offset(-(5 * repaint.value) / 2 - 30, 0));
      } else {
        _drawAxisValueText(canvas, leftList[i].valueString, offset: Offset(-(leftWidth * repaint.value) / 2, 0));
      }

      if (rightWidth < 40) {
        _drawAxisValueText(canvas, rightList[i].valueString, offset: Offset((5 * repaint.value) / 2 + 30, 0));
      } else {
        _drawAxisValueText(canvas, rightList[i].valueString, offset: Offset((rightWidth * repaint.value) / 2, 0));
      }

      canvas.restore();
    }

    canvas.restore();
  }

  void _drawAxisValueText(Canvas canvas, String str, {Offset offset = Offset.zero}) {
    TextSpan text = TextSpan(text: str, style: TextStyle(fontSize: 14, color: Color(0xFF333333)));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局

    Size size = _textPainter.size;

    Offset offsetPos = Offset(offset.dx - size.width / 2, offset.dy - size.height / 2);
    _textPainter.paint(canvas, offsetPos);
  }

  drawTitle(Canvas canvas, Size size) {
    canvas.save();

    //左边右边
    for (int i = 0; i < leftList.length; i++) {
      canvas.save();
      double dy = yStep * 0.75 / 2 + yStep * i;
      canvas.translate(0, dy);
      _drawAxisText(canvas, leftList[i].title, offset: Offset(0, 0), isLeft: true);
      _drawAxisText(canvas, rightList[i].title, offset: Offset(0, 0));
      canvas.restore();
    }

    canvas.restore();
  }

  void _drawAxisText(Canvas canvas, String str, {Offset offset = Offset.zero, bool isLeft = false}) {
    TextSpan text = TextSpan(text: str, style: TextStyle(fontSize: 12, color: Color(0xFF333333)));
    _textPainter.text = text;
    _textPainter.layout(maxWidth: 10); // 进行布局

    Size size = _textPainter.size;

    Offset offsetPos;
    if (isLeft) {
      offsetPos = Offset(offset.dx - size.width, offset.dy - size.height / 2);
    } else {
      offsetPos = Offset(offset.dx + size.width / 2, offset.dy - size.height / 2);
    }
    _textPainter.paint(canvas, offsetPos);
  }

  drawLeft(Canvas canvas, Size size) {
    fillPaint..color = leftColors;
    canvas.save();
    canvas.translate(0, yStep);
    for (int i = 0; i < leftList.length; i++) {
      double dataWidth = leftList[i].proportion * (maxWidth / 2);
      canvas.drawRect(Rect.fromLTWH(-dataWidth * repaint.value, 0, dataWidth * repaint.value, yStep * 0.75).translate(0, -yStep), fillPaint);

      canvas.translate(0, yStep);
    }

    canvas.restore();
  }

  drawRight(Canvas canvas, Size size) {
    fillPaint..color = rightColors;
    canvas.save();
    canvas.translate(0, yStep);

    for (int i = 0; i < leftList.length; i++) {
      double dataWidth = rightList[i].proportion * (maxWidth / 2);
      canvas.drawRect(Rect.fromLTWH(0, 0, dataWidth * repaint.value, yStep * 0.75).translate(0, -yStep), fillPaint);

      canvas.translate(0, yStep);
    }

    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
